package com.baizhi.action;

import com.baizhi.entity.Atm;
import com.baizhi.service.AtmService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

@Controller
@Scope("prototype")
public class AtmAction {
    @Autowired
    private AtmService atmService;

    public void setAtmService(AtmService atmService) {
        this.atmService = atmService;
    }
    private int pageNum=1;
    private int pageSize=5;
    private int id;
    private Atm atm;
    public void showAll() throws IOException {
        List<Atm> list = atmService.selectAll(pageNum, pageSize);
        PageInfo<Atm> atmPageInfo = new PageInfo<>(list, 10);
        int[] pages = atmPageInfo.getNavigatepageNums();
        HashMap<String, Object> map = new HashMap<>();

        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();

        map.put("list",list);
        map.put("pages",pages);
        ObjectMapper mapper = new ObjectMapper();
        String mapJson = mapper.writeValueAsString(map);
        pw.print(mapJson);
        pw.flush();
    }
    public void deleteOne() throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        HashMap<String, Object> map = new HashMap<>();
        try {
            atmService.deleteById(id);
            map.put("status","success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","failed");
        }
        ObjectMapper mapper = new ObjectMapper();
        String mapJson = mapper.writeValueAsString(map);
        pw.print(mapJson);
        pw.flush();
    }

    public void addOne() throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        HashMap<String, Object> map = new HashMap<>();
        try {
            atmService.insertAtm(atm);
            map.put("status","success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","failed");
        }
        ObjectMapper mapper = new ObjectMapper();
        String mapJson = mapper.writeValueAsString(map);
        pw.print(mapJson);
        pw.flush();
    }

    public void updateOne() throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        HashMap<String, Object> map = new HashMap<>();
        try {
            atmService.updateAtm(atm);
            map.put("status","success");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status","failed");
        }
        ObjectMapper mapper = new ObjectMapper();
        String mapJson = mapper.writeValueAsString(map);
        pw.print(mapJson);
        pw.flush();
    }
    public void showOne() throws IOException {
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        HashMap<String, Object> map = new HashMap<>();
        Atm atm = atmService.selectById(id);
        map.put("atm",atm);
        ObjectMapper mapper = new ObjectMapper();
        String mapJson = mapper.writeValueAsString(map);
        pw.print(mapJson);
        pw.flush();
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Atm getAtm() {
        return atm;
    }

    public void setAtm(Atm atm) {
        this.atm = atm;
    }
}
